package com.yumeng.framework.auth.security.authentication;

import com.yumeng.common.api.ApiCode;
import com.yumeng.common.utils.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 身份认证入口点（JSON）（用于前后端分离）
 *
 * @author wxd
 * @date 2025/2/18 14:23
 */
public class JsonAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
/*        Throwable cause = authException.getCause();
        if (cause != null) {
            if (cause instanceof ServletException) {
                //...
            }
        }*/
        ResponseUtils.writeJson(response, ApiCode.UNAUTHORIZED, String.format("未认证：%s", authException.getMessage()));
    }

}
